package org.kjtc.web;

import org.kjtc.commom.CommonUtil;
import org.kjtc.commom.XmlSerializer;
import org.kjtc.entity.WeChatConfig;
import org.kjtc.entity.WeChatInfo;
import org.kjtc.service.TimerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.sql.Time;

@Component("instantiationTracingBeanPostProcessor")
public class InstantiationTracingBeanPostProcessor implements
        ApplicationListener<ContextRefreshedEvent> {

    @Resource(name = "weChatInfo")
    private WeChatInfo weChatInfo;

    @Autowired
    private TimerService timerService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if(event.getApplicationContext().getParent() == null){
            File file = new File(CommonUtil.WECHAT_CONFIG_PATH);
            if (file.exists()) {
                XmlSerializer<WeChatConfig> xSerializer = new XmlSerializer<WeChatConfig>();
                try {
                    weChatInfo.setAccess(xSerializer.unMarshallXml(file, WeChatConfig.class).getAccess());
                    weChatInfo.setPushTime(xSerializer.unMarshallXml(file, WeChatConfig.class).getPushTime());
                    weChatInfo.setPushType(xSerializer.unMarshallXml(file, WeChatConfig.class).getPushType());
                    weChatInfo.setPushUrl(xSerializer.unMarshallXml(file, WeChatConfig.class).getPushUrl());
                    weChatInfo.setWeChatModuleID(xSerializer.unMarshallXml(file, WeChatConfig.class).getWeChatModuleID());
                    weChatInfo.setForwardUrl(xSerializer.unMarshallXml(file, WeChatConfig.class).getForwardUrl());
                    weChatInfo.setStatisticsTime(xSerializer.unMarshallXml(file,WeChatConfig.class).getStatisticsTime());
                    timerService.sendWorkDataEveryDay();
                    timerService.insertBackLogDataEverybody();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
